package com.study.zzedu.ui.exercise.activity;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AlertDialog;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.study.zzedu.R;
import com.study.zzedu.utils.AppConstant;
import com.study.zzedu.utils.LogUtil;
import com.study.zzedu.utils.OkHttpUtils;
import com.study.zzedu.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * @version 1.0
 * @author hzc
 * @date 2015-6-24
 */


public class IntroduceItemActivity extends Activity {

	int index ;
	LocalBroadcastManager mLocalBroadcastManager;
	TextView titleName,title1,title2;
//	public IntroduceItemActivity(int index){
//		this.index = index;
//	}
private RelativeLayout btn_back;
	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
		setContentView(R.layout.pager_item_coruse_info);

		Button btnGo = (Button) findViewById(R.id.course_item_go);
		btnGo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent intent = new Intent(IntroduceItemActivity.this,QuestionItemActivity.class);
				startActivity(intent);
				finish();
			}
		});
		btn_back = findViewById(R.id.rl_back);
		btn_back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				finish();
			}
		});
		titleName = (TextView) findViewById(R.id.course_item_name);
		title1 = (TextView) findViewById(R.id.course_item_title1);
		title2 = (TextView) findViewById(R.id.course_item_title2);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if(TextUtils.isEmpty(AppConstant.courseCode) || TextUtils.isEmpty(AppConstant.chapterCode)){
			ToastUtil.showShortToast("参数异常");
		}else {
			queryCourseTestScore(this, AppConstant.courseCode,AppConstant.chapterCode);
		}
	}

	public void queryCourseTestScore(Context context, String courseCode, String chapterCode){
		OkHttpUtils.getInstance(context).getJsonForString(OkHttpUtils.BASE_URL + "/appCourseSubject/updateCheckChaptersTest",
				new String[]{"courseCode","chapterCode"}, new String[]{courseCode,chapterCode}, new OkHttpUtils.HttpCallBack() {
					@Override
					public void onSuccess(String obj) {
						try {
							JSONObject jObject = new JSONObject(obj);
							if (0 == jObject.getInt("code")) {
								LogUtil.e("Forget", "在线测试-测试中——根据章节号查询已测试次数和最高分成功");

								JSONArray jArray = jObject.getJSONArray("data");
								if (!(jArray.length() > 0)) {
									ToastUtil.showShortToast("无数据");
									return;
								} else {
									gotTestCount = (float) jArray.getJSONObject(0).getDouble("testCount");
									bestSocre = (float) jArray.getJSONObject(0).getDouble("testScore");
									subjectCount = (float)jArray.getJSONObject(0).getDouble("subjectCount");

									updataView();
								}

							} else {
								LogUtil.e("Forget", "在线测试-测试中——根据章节号查询已测试次数和最高分失败");
							}
						} catch (Exception e) {
							e.toString();
						}
					}

					@Override
					public void onSuccess(JSONObject obj) {
						//resultListener.onSuccess(obj.toString());
					}

					@Override
					public void onError(String msg) {
						LogUtil.e("Forget", "在线测试-测试中——根据章节号查询已测试次数和最高分失败");
					}
				});
	}
	public float gotTestCount = 2, bestSocre = 3,subjectCount = 0;
	private void updataView(){

		titleName.setText(AppConstant.chapterName);

		SpannableString textSpanned1 = new SpannableString(getResources().getString(R.string.test_title4)+gotTestCount);
		textSpanned1.setSpan(new ForegroundColorSpan(this.getResources().getColor(R.color.bg_orange)),
				getResources().getString(R.string.test_title4).length(), (getResources().getString(R.string.test_title4)+gotTestCount).length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		title1.setText(textSpanned1);

		SpannableString textSpanned2 = new SpannableString(getResources().getString(R.string.test_title5)+bestSocre);
		textSpanned2.setSpan(new ForegroundColorSpan(this.getResources().getColor(R.color.bg_orange)),
				getResources().getString(R.string.test_title5).length(), (getResources().getString(R.string.test_title5)+bestSocre).toString().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		title2.setText(textSpanned2);

		if(0 == subjectCount){
			showDialog();
		}
	}

	private void showDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		AlertDialog mDialog = builder.setMessage("没有查到测试题！")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				})
				.setCancelable(false)
				.create();
		mDialog.show();
	}
}